//
//  QSWelcomeViewController.m
//  SinaWeiBo
//
//  Created by apple on 16/6/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSWelcomeViewController.h"
#import "QSUserAccountViewModel.h"
#import "QSUserAccount.h"

@interface QSWelcomeViewController ()
/**
 *  头像
 */
@property (nonatomic, strong) UIImageView *iconView;

/**
 *  欢迎标语
 */
@property (nonatomic, strong) UILabel *welcomeLable;

/**
 *  iconView的高度约束
 */
@property (nonatomic, strong) NSLayoutConstraint *iconHeightCons;
@end

@implementation QSWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 判断用户是否登录，设置头像图片
    QSUserAccountViewModel *accountModel = [[QSUserAccountViewModel alloc] init];
    if (accountModel.UserLongin) {  // 用户已经登录
        // 从沙盒中获取用户头像URL
        NSString *urlStr = [[QSUserAccountViewModel alloc] init].account.avatar_large;
        NSURL *url = [NSURL URLWithString:urlStr];
        
        // 设置头像
        [self.iconView sd_setImageWithURL:url];
    }
}

/**
 *  重写loadView，搭建界面
 */
- (void)loadView
{
    // 初始化view
    self.view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    
    // 添加控件
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.welcomeLable];
    
    // 自动布局
    [self setupLayout];
}

/**
 *  以动画的形式显示
 */
// 不能在viewDidLoad中实现动画
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 动画显示
    [self startAnimation];
}

- (void)startAnimation
{
    // iconView的约束
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.iconHeightCons = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:160];
    
    [self.view addConstraint:self.iconHeightCons];
    
    // welcomeLable
    self.welcomeLable.alpha = 0;
    
    // 动画
    [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionTransitionNone animations:^{
        
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionTransitionNone animations:^{
            self.welcomeLable.alpha = 1;
        } completion:^(BOOL finished) {
            // 利用通知跳转到主界面
            [[NSNotificationCenter defaultCenter] postNotificationName:AppSwitchRootViewControler object:nil];
        }];
    }];
}

/**
 *  自动布局
 */
- (void)setupLayout
{
    // 关闭autoResize（因为autoLayout和autoResize不能共存）
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    self.welcomeLable.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 要在view上才能对控件进行约束（因为控件是加在view上的）
    /**
     *  iconView
     */
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    // 加载沙盒中的头像是要添加头像的宽高
    [self.iconView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:90]];
    [self.iconView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:90]];
    
    /**
     *  使用约束动画是，自动布局这里就不用设置要用动画显示的约束，在动画中创建约束即可
     */
//    self.iconHeightCons = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:160];
//    [self.view addConstraint:self.iconHeightCons];
    
    /**
     *  welcomeLable
     */
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.welcomeLable attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.welcomeLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
}

#pragma mark - 懒加载
/**
 *  头像
 */
- (UIImageView *)iconView
{
    if ((!_iconView)) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
        
        // 设置圆角
        [self.iconView.layer setCornerRadius:45];
        
        // 显示边界
        [self.iconView.layer setMasksToBounds:YES];
    }
    
    return _iconView;
}

/**
 *  欢迎标语
 */
- (UILabel *)welcomeLable
{
    if (!_welcomeLable) {
        _welcomeLable = [[UILabel alloc] init];
        
        self.welcomeLable.text = [NSString stringWithFormat:@"%@, 欢迎归来", [[QSUserAccountViewModel alloc] init].account.name];
        [self.welcomeLable setTextColor:[UIColor lightGrayColor]];
        [self.welcomeLable setFont:[UIFont systemFontOfSize:18]];
        [self.welcomeLable setTextAlignment:NSTextAlignmentCenter];
        self.welcomeLable.alpha = 0;
        [self.welcomeLable sizeToFit];
    }
    
    return _welcomeLable;
}
@end
